#include "../bsp/can/can.h"
#include "../bsp/clk/bsp_clk.h"
#include "../bsp/delay/bsp_delay.h"
#include "../bsp/exit/bsp_exit.h"
#include "../bsp/int/bsp_int.h"
#include "../bsp/led/bsp_led.h"
#include "../stdio/include/stdio.h"


/**
 * @brief main函数
 *
 * @return int
 */
int main(void) {

  int_init();      /* 初始化中断(一定要最先调用！) */
  imx6u_clkinit(); /* 初始化系统时钟 			*/
  clk_enable();    /* 使能所有的时钟 			*/
  led_init();      /* 初始化led 			*/



  can_gpio_init();
  can_controller_init(CAN1);
  can_protocol_timing_init(CAN1);

  uint8_t tx[] = {1, 1, 1, 1, 0, 0, 0, 1};
  uint8_t size = sizeof(tx) / sizeof(tx[0]);
  uint8_t rx[8] = {0};

  can_frame_t rx_frame;
  rx_frame.buf = rx;
  rx_frame.frame_id = 11;
  rx_frame.index = 15;
  rx_frame.size = 8;

  can_frame_t tx_frame;
  tx_frame.frame_id = 12;
  tx_frame.buf = tx;
  tx_frame.index = 18;
  tx_frame.size = size;

  uint8_t j = 0;
  can_rx_mb_config(CAN1, rx_frame.index, rx_frame.frame_id);
  can_interrupt_init(CAN1, &rx_frame);

  while (1) {

    can_send_data(CAN1, &tx_frame);
    if (read_data_flag) {
      for (j = 0; j < rx_frame.size; j++) {
        printf(" %d ", rx_frame.buf[j]);
      }
      printf("\r\n");
      read_data_flag = 0;
    }
  }

  return 0;
}
